﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ClickerClimber.Models;
using ClickerClimber.Managers;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace ClickerClimber.Scenes
{
    public class HUDScene : IGameObject, IGameObjectDrawable
    {
        private SpriteFont _font;

        private Texture2D _scoreTexture;
        private Texture2D _timeTexture;
        private int _score;
        private int _high;
        private float _timer;

        public void Initialize()
        {
            _score = 0;
            _high = GameParameters.BOARD_INITIAL_ROWS;
            _timer = GameParameters.GAME_TIME;
        }

        public void Update(GameTime gameTime)
        {
            _timer -= gameTime.ElapsedGameTime.Milliseconds/1000.0f;
            AudioManager.Instance.UpdateMusicPitch(_high);
        }

        public void LoadTextures(ContentManager content)
        {
            //_font = content.Load<SpriteFont>("Fonts/font");
            _font = content.Load<SpriteFont>("Fonts/Rustler");
            _scoreTexture = content.Load<Texture2D>(GameParameters.SCORE_TEXTURE_NAME);
            _timeTexture = content.Load<Texture2D>(GameParameters.TIME_TEXTURE_NAME);
        }

        public void Draw(SpriteBatch batch)
        {
            DrawScore(batch);
            DrawHigh(batch);
            DrawTime(batch);
        }

        public void AddScore(int score)
        {
            _score += score;
        }

        #region Properties

        public int Hight
        {
            set
            {
                _high = value;
            }
            get
            {
                return _high;
            }
        }

        public bool Timeout
        {
            get
            {
                return _timer<=0.0f;
            }
        }

        public float Time
        {
            get
            {
                return _timer;
            }
        }

        #endregion Properties


        #region Support methods

        private void DrawScore(SpriteBatch batch)
        {
            string pointsText = GameParameters.SCORE_TEXT;
            string pointsNumberText = "" + _score;

            // Find the center of the string
            Vector2 fontOrigin = _font.MeasureString(pointsText) / 2;
            // Draw the string
            //batch.DrawString(_font, pointsText, GameParameters.SCORE_OFFSET, Color.Black,
            //    0.0f, fontOrigin, 1.0f, SpriteEffects.None, 0.5f);

            batch.Draw(_scoreTexture, GameParameters.SCORE_OFFSET, Color.White);

            fontOrigin = _font.MeasureString(pointsNumberText) / 2;
            batch.DrawString(_font, pointsNumberText, GameParameters.SCORE_OFFSET+new Vector2(_scoreTexture.Width*0.5f, 57), Color.WhiteSmoke,
                 0.0f, fontOrigin, 1.0f, SpriteEffects.None, 0.5f);
        }

        private void DrawHigh(SpriteBatch batch)
        {
            string highText = GameParameters.HIGH_TEXT;
            string highNumberText = "" + _high;

            // Find the center of the string
            Vector2 fontOrigin = _font.MeasureString(highText) / 2;
            // Draw the string
            //batch.DrawString(_font, highText, GameParameters.HIGH_OFFSET, Color.Black,
            //    0.0f, fontOrigin, 1.0f, SpriteEffects.None, 0.5f);
            //batch.Draw(_timeTexture, GameParameters.HIGH_OFFSET, Color.White);
            fontOrigin = _font.MeasureString(highNumberText) / 2;
            batch.DrawString(_font, highNumberText, GameParameters.HIGH_OFFSET +new Vector2(0, 140), Color.Black,
                 0.0f, fontOrigin, 1.0f, SpriteEffects.None, 0.5f);
        }

        private void DrawTime(SpriteBatch batch)
        {
            int minutes =(int)( _timer / 60.0f);
            int seconds = (int)_timer - minutes * 60;
            string secondsText = ""+seconds;
            if (seconds < 10){
                secondsText = "0" + seconds;
            }
            string timeNumberText = "" + minutes + ":" + secondsText;
            batch.Draw(_timeTexture, GameParameters.TIME_OFFSET, Color.White);
            Vector2 fontOrigin = _font.MeasureString(timeNumberText) / 2;
            if (minutes == 0 && seconds <= 15)
            {
                batch.DrawString(_font, timeNumberText, GameParameters.TIME_OFFSET + new Vector2(_timeTexture.Width * 0.5f, 52), Color.Yellow,
                 0.0f, fontOrigin, 1.65f, SpriteEffects.None, 0.5f);
            }
            else
            {
                batch.DrawString(_font, timeNumberText, GameParameters.TIME_OFFSET + new Vector2(_timeTexture.Width * 0.5f, 52), Color.White,
                 0.0f, fontOrigin, 1.65f, SpriteEffects.None, 0.5f);
            }
            
        }
        #endregion Support methods
    }
}
